<template>
  <a-card :bordered="false">
    <div style="display: flex; justify-content: space-between">
      <!-- 操作按钮区域 -->
      <div class="table-operator">
        <a-button @click="handleAdd" type="primary" icon="plus" v-has="'agency:add'">新增</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1" @click="batchDel"><a-icon type="delete" v-has="'agency:delete'" />删除</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
        </a-dropdown>
      </div>
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="服务商编码" style="margin-right: 8px">
                <a-input placeholder="请输入服务商编码" v-model="queryParam.agencyCode"></a-input>
              </a-form-item>
            </a-col>
            <!-- 省市县级联 -->
            <a-row :gutter="24">
              <a-select
                v-model="queryParam.province"
                style="width: 120px; margin-right: 8px"
                :options="formatData(this.allList)"
                @change="cityClick"
                placeholder="请选择省"
              />
              <a-select
                v-model="queryParam.city"
                style="width: 120px; margin-right: 8px"
                :options="formatData(this.citys)"
                @change="citysClicks"
                placeholder="请选择市"
              />
              <a-select
                v-model="queryParam.area"
                style="width: 120px; margin-right: 8px"
                :options="formatData(this.cityss)"
                placeholder="请选择区"
                @change="clicks(model.area)"
              />
            </a-row>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
<!--                <a-button type="primary" icon="download" @click="handleExportXls('服务商表')" style="margin-left: 8px"-->
<!--                  >导出</a-button-->
<!--                >-->
<!--                <a-upload-->
<!--                  name="file"-->
<!--                  :showUploadList="false"-->
<!--                  :multiple="false"-->
<!--                  :headers="tokenHeader"-->
<!--                  :action="importExcelUrl"-->
<!--                  @change="handleImportExcel"-->
<!--                >-->
<!--                  <a-button type="primary" icon="import" style="margin-left: 8px">导入</a-button>-->
<!--                </a-upload>-->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :scroll="{ x: 'auto' }"
        class="j-table-force-nowrap"
        @change="handleTableChange"
      >
        <!-- 状态 -->
        <template slot="status" slot-scope="text, record">
          <a-switch
            default-checked
            :checked="record.status"
            @click="statusEditChange(record, record.status, 'status')"
          />
        </template>
        <!-- 状态 -->
<!--        <template slot="royaltyStatus" slot-scope="text, record">-->
<!--          <a-switch-->
<!--            default-checked-->
<!--            :checked="record.royaltyStatus"-->
<!--            @click="statusEditChange(record, record.royaltyStatus, 'royaltyStatus')"-->
<!--          />-->
<!--        </template>-->
        <!-- 营业执照 -->
        <template slot="businessLicense" slot-scope="text, record">
          <div class="anty-img-wrap">
            <j-image-upload v-if="record.businessLicense" text="上传" disabled v-model="record.businessLicense"></j-image-upload>
          </div>
        </template>

        <!-- 身份证照片 -->
        <template slot="idCardImg" slot-scope="text, record">
          <div class="anty-img-wrap">
            <j-image-upload v-if="record.idCardImg" text="上传" disabled v-model="record.idCardImg"></j-image-upload>
          </div>
        </template>

        <!-- 身份证反面 -->
        <template slot="idCardReverseImg" slot-scope="text, record">
          <div class="anty-img-wrap">
            <j-image-upload v-if="record.idCardReverseImg" text="上传" disabled v-model="record.idCardReverseImg"></j-image-upload>
          </div>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)" v-has="'agency:edit'">编辑</a>
          <a-divider type="vertical" v-has="'agency:edit'" />
          <!-- <a @click="mapEdit(record)" v-has="'user:map'">定位</a>
          <a-divider type="vertical" v-has="'user:map'"/> -->
          <a @click="userEdit(record)" v-has="'user:binding'">绑定后台账号</a>
          <a-divider type="vertical" v-has="'user:binding'" />
          <a-dropdown v-has="'agency:delete'">
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <xhdjAgency-modal ref="modalForm" @ok="modalFormOk" :all="this.all"></xhdjAgency-modal>
    <mapdialog ref="mapdialog" :all="this.all" @ok="modalFormOk"></mapdialog>
    <userDialog ref="userDialog" @ok="modalFormOk"></userDialog>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import Area from '@/components/_util/Area'
import XhdjAgencyModal from './modules/XhdjAgencyModal'
import mapdialog from './modules/mapdialog'
import userDialog from './modules/userDialog.vue'
import { getFileAccessHttpUrl } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { httpAction } from '@/api/manage'
import Speech from 'speak-tts'
export default {
  name: 'XhdjAgencyList',
  mixins: [JeecgListMixin],
  components: {
    XhdjAgencyModal,
    mapdialog,
    userDialog,
  },
  data() {
    return {
      speech: null,
      description: '服务商表管理页面',
      queryParam: {
        agencyCode: '',
        province: undefined,
        area: undefined,
        city: undefined,
      },
      // 表头
      columns: [
        {
          title: '服务商编码',
          align: 'center',
          dataIndex: 'agencyCode',
        },
        {
          title: '服务商名称',
          align: 'center',
          dataIndex: 'agencyName',
        },
        {
          title: '省',
          align: 'center',
          dataIndex: 'province',
          customRender: (text, record) => {
            return this.cityGS(record.province)
          },
        },
        {
          title: '市',
          align: 'center',
          dataIndex: 'city',
          customRender: (text, record) => {
            return this.cityGS(record.city)
          },
        },

        {
          title: '是否是总代理',
          align: 'center',
          dataIndex: 'isAdmin',
          customRender: (text, record) => {
            return record.isAdmin == 1 ? '是' : '否'
          },
        },

        {
          title: '服务商类型',
          align: 'center',
          dataIndex: 'shopType',
          customRender: (text, record) => {
            return record.shopType == 0 ? '直营 ' : '加盟'
          },
        },
        {
          title: '运营状态',
          align: 'center',
          dataIndex: 'operationState',
          customRender: (text, record) => {
            return record.operationState == 1 ? '运营中' : '暂停服务'
          },
        },
        {
          title: '负责人姓名',
          align: 'center',
          dataIndex: 'principalName',
        },
        {
          title: '负责人手机号',
          align: 'center',
          dataIndex: 'principalPhone',
        },
        {
          title: '身份证号',
          align: 'center',
          dataIndex: 'idCard',
        },
        {
          title: '分红比例',
          align: 'center',
          dataIndex: 'serveRate',
          customRender: (text, record) => {
            return record.serveRate?record.serveRate+ '%':"";
          },
        },
        // {
        //   title: '余额', 以后在做备用功能
        //   align: 'center',
        //   dataIndex: 'balance',
        // },
        {
          title: '营业执照',
          align: 'center',
          dataIndex: 'businessLicense',
          width: 120,
          scopedSlots: { customRender: 'businessLicense' },
        },
        {
          title: '身份证正面',
          align: 'center',
          width: 120,
          dataIndex: 'idCardImg',
          scopedSlots: { customRender: 'idCardImg' },
        },
        {
          title: '身份证正面',
          align: 'center',
          width: 120,
          dataIndex: 'idCardReverseImg',
          scopedSlots: { customRender: 'idCardReverseImg' },
        },
        {
          title: '绑定后台账号',
          align: 'center',
          width: 120,
          dataIndex: 'sysUserName',
        },
        {
          title: '客服电话',
          align: 'center',
          dataIndex: 'agencyServicePhone',
        },
        {
          title: '在线客服链接地址',
          align: 'center',
          dataIndex: 'agencyServiceUrl',
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
        },
        // {
        //   title: '提成开关',
        //   align: 'center',
        //   dataIndex: 'royaltyStatus',
        //   scopedSlots: { customRender: 'royaltyStatus' },
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/xhdjAgency/queryPageList',
        delete: '/xhdjAgency/delete',
        deleteBatch: '/xhdjAgency/deleteBatch',
        exportXlsUrl: 'xhdjAgency/exportXls',
        importExcelUrl: 'xhdjAgency/importExcel',
      },
      all: [], // 地址信息
      allList: [], // 省份数据
      citys: [],
      cityss: [],
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  created() {
    this.all = new Area().all
    this.allList = this.all.filter((res) => res.index == 1)
    this.queryParam = {
      agencyCode: '',
      province: undefined,
      area: undefined,
      city: undefined,
    }
    this.speech = new Speech()
    this.speech.setLanguage('zh-CN')
    this.speech.init().then(() => {})
  },
  methods: {
    getAvatarView: function (avatar) {
      return getFileAccessHttpUrl(avatar)
    },
    // 下拉框格式转换
    formatData(array) {
      var arr = []
      arr = array.forEach((res) => {
        res.value = res.id
        res.label = res.text
      })
      return array
    },
    // 城区中文显示
    cityGS(value) {
      var a = []
      a = this.all.filter((res) => res.id == value)
      if (a && a.length > 0) {
        return a[0].text
      } else {
        return '无'
      }
    },
    mapEdit(record) {
      this.$refs.mapdialog.edit(record)
      this.$refs.mapdialog.title = '定位'
      // this.$refs.modalmapdialogForm.disableSubmit = false;
    },
    statusEditChange(record, value, type) {
      record[type] = value == 0 ? 1 : 0
      httpAction('/xhdjAgency/edit', record, 'put')
        .then((res) => {
          if (res.success) {
            this.$message.success(res.message)
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.loadData()
        })
    },
    userEdit(record) {
      this.$refs.userDialog.edit(record)
      this.$refs.userDialog.title = '用户绑定'
      // this.$refs.modalmapdialogForm.disableSubmit = false;
    },
    cityClick() {
      this.queryParam.area = null
      this.queryParam.city = null
      this.citys = this.all.filter((res) => res.pid == this.queryParam.province)
    },
    citysClicks() {
      this.queryParam.area = null
      this.cityss = this.all.filter((res) => res.pid == this.queryParam.city)
    },
    clicks(value) {
      this.$forceUpdate()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';

.ant-row {
  display: flex;
}
.anty-img-wrap /deep/ .ant-upload-picture-card-wrapper {
  display: block !important;
}
</style>
